package com.itheima.em.server.service.impl.tianditu;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.itheima.em.config.BaiduServerConfig;
import com.itheima.em.config.EagleConfig;
import com.itheima.em.config.TMapServerConfig;
import com.itheima.em.enums.ProviderType;
import com.itheima.em.server.service.impl.baidu.BaiduWebApiService;
import com.itheima.em.service.DirectionService;
import com.itheima.em.service.EagleOrdered;
import com.itheima.em.vo.CoordinateVo;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 天地图的实现类
 */
@Service("TMapDirectionService")
@ConditionalOnBean(TMapServerConfig.class)
public class TMapDirectionServiceImpl implements DirectionService {

    @Resource
    private TMapWebApiService tMapWebApiService;
    @Resource
    private EagleConfig eagleConfig;

    /**
     * 驾车路线规划
     *
     * @param origin      起点经纬度
     * @param destination 目的地经纬度
     * @param param       百度/高德的可选参数，如需要请根据官方文档添加参数
     *                    高德：https://lbs.amap.com/api/webservice/guide/api/newroute#t5
     *                    百度：https://lbsyun.baidu.com/index.php?title=webapi/directionlite-v1#service-page-anchor-1-0
     *                    天地图：http://lbs.tianditu.gov.cn/server/drive.html
     * @return
     */
    @Override
    public String driving(CoordinateVo origin, CoordinateVo destination, Map<String, Object> param) {
        String url = this.eagleConfig.getTmapWebApi() + "/drive";
        return this.execute(url, origin, destination, param);
    }

    /**
     * 步行路线规划
     *
     * @param origin      起点经纬度
     * @param destination 目的地经纬度
     * @param param       百度/高德的可选参数，如需要请根据官方文档添加参数
     *                    高德：https://lbs.amap.com/api/webservice/guide/api/newroute#t6
     *                    百度：https://lbsyun.baidu.com/index.php?title=webapi/directionlite-v1#service-page-anchor-1-2
     * @return
     */
    @Override
    public String walking(CoordinateVo origin, CoordinateVo destination, Map<String, Object> param) {
        String url = this.eagleConfig.getTmapWebApi() + "/drive";
        if (CollUtil.isEmpty(param)) {param = new HashMap<>();}
        param.put("style",3);//步行标识
        return this.execute(url, origin, destination, param);
    }

    /**
     * 骑行路线规划
     *
     * @param origin      起点经纬度
     * @param destination 目的地经纬度
     * @param param       百度/高德的可选参数，如需要请根据官方文档添加参数
     *                    高德：https://lbs.amap.com/api/webservice/guide/api/newroute#t7
     *                    百度：https://lbsyun.baidu.com/index.php?title=webapi/directionlite-v1#service-page-anchor-1-1
     * @return 百度/高德响应的数据
     */
    @Override
    public String bicycling(CoordinateVo origin, CoordinateVo destination, Map<String, Object> param) {
        String url = this.eagleConfig.getTmapWebApi() + "/drive";
        if (CollUtil.isEmpty(param)) {param = new HashMap<>();}
        param.put("style",1);//最短路线标识
        return this.execute(url, origin, destination, param);
    }



    private String execute(String url, CoordinateVo origin, CoordinateVo destination, Map<String, Object> param) {
        Map<String, Object> postStrMap = new HashMap<>();
        postStrMap.put("orig", origin.getLongitude() + "," + origin.getLatitude()); //起点经纬度
        postStrMap.put("dest", destination.getLongitude() + "," + destination.getLatitude()); //终点经纬度
        postStrMap.put("style", 0); //默认0 （0：最快路线，1：最短路线，2：避开高速，3：步行）
        if (CollUtil.isNotEmpty(param)) {
            //将用户的可选参数加入到请求参数列表中
            for (Map.Entry<String, Object> entry : param.entrySet()) {
                postStrMap.put(entry.getKey(), entry.getValue());
            }
        }
        //封装请求参数
        Map<String, Object> requestParam = new HashMap<>();
        requestParam.put("postStr", JSONUtil.toJsonStr(postStrMap));
        requestParam.put("type","search");

        //执行请求
        return this.tMapWebApiService.doGet(url, requestParam, response -> {
            if (!response.isOk()) {
                return null;
            }
            String body = response.body();
            return body;
//            JSONObject jsonObject = JSONUtil.parseObj(body);
//            if (jsonObject.getInt("status") != 0) {
//                //出错，将错误码和错误信息返回
//                String message = jsonObject.getStr("message");
//                return StrUtil.format("error: message = {}", message);
//            }
//            return jsonObject.getJSONObject("result").toString();

        });
    }

    @Override
    public int getOrder() {
        return EagleOrdered.ONE;
    }
    @Override
    public ProviderType getProvider() {
        return ProviderType.TMAP;
    }
}
